<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Mail.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Mail.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Mails.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_MailController extends Zend_Controller_Action {
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelMail;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelMail = new End_Models_Mails();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {

        $form = new Application_Form_Mail();

        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $template_id = $form->getValue('template_id');
                $subject = $form->getValue('subject');
                $content = $form->getValue('content');
                $created_user_id = $form->getValue('created_user_id');
                $from_mail = $form->getValue('from_mail');
                $from_name = $form->getValue('from_name');
                $reply_to_mail = $form->getValue('reply_to_mail');
                $reply_to_name = $form->getValue('reply_to_name');
                $to_mail = $form->getValue('to_mail');
                $to_name = $form->getValue('to_name');
                $status = $form->getValue('status');
                $created_date = $form->getValue('created_date');
                $sent_date = $form->getValue('sent_date');

                $mail = new Mail(null, $template_id, $subject, $content, $created_user_id, $from_mail, $from_name, $reply_to_mail, $reply_to_name, $to_mail, $to_name, $status, $created_date, $sent_date);

                $this->modelMail->_insert($mail);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $mail_id = (int) $this->_getParam('mail_id');
                $this->modelMail->_delete($mail_id);
            }
            $this->_helper->redirector('index');
        } else {
            $mail_id = $this->_getParam('mail_id', 0);
            $this->view->Mail = Utility_Converter::objectToArray($this->modelMail->_find($mail_id));
        }
    }

    public function editAction() {
        $mail_id = $this->_getParam('mail_id');
        $form = new Application_Form_Mail();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }

            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $template_id = $form->getValue('template_id');
                $subject = $form->getValue('subject');
                $content = $form->getValue('content');
                $created_user_id = $form->getValue('created_user_id');
                $from_mail = $form->getValue('from_mail');
                $from_name = $form->getValue('from_name');
                $reply_to_mail = $form->getValue('reply_to_mail');
                $reply_to_name = $form->getValue('reply_to_name');
                $to_mail = $form->getValue('to_mail');
                $to_name = $form->getValue('to_name');
                $status = $form->getValue('status');
                $created_date = $form->getValue('created_date');
                $sent_date = $form->getValue('sent_date');

                $mail = new Mail($mail_id, $template_id, $subject, $content, $created_user_id, $from_mail, $from_name, $reply_to_mail, $reply_to_name, $to_mail, $to_name, $status, $created_date, $sent_date);
                $Mail = new End_Models_Mails();
                $Mail->_update($mail);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $mail_id = (int) $this->_getParam('mail_id', 0);
            if ($mail_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelMail->_find($mail_id)));
            }
        }
    }

    public function indexAction() {
        $mail = $this->modelMail->fetchAll();
        $this->view->Mail = $mail;
    }

}

